************
* PURPOSE:  Create alternative Table 1 that uses and tests ACS measures 
************
global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"
cap log close
log using $SSDIMed/results/tables/16_tab1ACS.txt, replace text 


***column 1: same as Table 1 
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
keep if inrange(age_year_covstart_fill, 22,62)
reghdfe incidence_pop_age_atapp c.unemp_rate_county_atapp [aw=pop_age_atapp] , abs(fipscounty_firstnm_g) cluster(county_mofd)
qui sum unemp_rate_county_atapp
gen Zunemp_rate_county_atapp=(unemp_rate_county_atapp-`r(mean)')/`r(sd)'
reghdfe incidence_pop_age_atapp c.Zunemp_rate_county_atapp [aw=pop_age_atapp] , abs(fipscounty_firstnm_g) cluster(county_mofd)
sum incidence_pop_age_atapp [aw=pop_age_atapp]
sum Zunemp_rate_county_atapp
****PANEL B 
keep if inrange(age_year_covstart_fill, 51,52) 
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
reghdfe incidence_pop_age_atapp age52 UR age52xUR [aw=pop_age_atapp] , abs(fipscounty_firstnm_g) cluster(county_mofd)


***column 2: combine ages so that N=county X month 
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
keep if inrange(age_year_covstart_fill, 22,62)
gcollapse (sum) count pop_age_atapp (mean) unemp_rate_county_atapp, by(covstart_month fipscounty_firstnm_g county_mofd) fast 
gen incidence=10^6*count/pop_age_atapp
local var unemp_rate_county_atapp
reghdfe incidence c.`var' [aw=pop_age_atapp], abs(fipscounty_firstnm_g) cluster(county_mofd)
qui sum `var'
gen Z`var'=(`var'-`r(mean)')/`r(sd)'
sum Z`var'
reghdfe incidence c.Z`var' [aw=pop_age_atapp], abs(fipscounty_firstnm_g) cluster(county_mofd)

***column 3:  full pop, county X year variation 
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
keep if inrange(age_year_covstart_fill, 22,62)
gcollapse (sum) count pop_age_atapp, by(covstart_month covstart_year fipscounty_firstnm county_year) fast
gcollapse (sum) count (mean) pop_age_atapp, by(covstart_year fipscounty_firstnm county_year) fast
rename fipscounty_firstnm fipscounty
merge 1:1 covstart_year fipscounty using "$SSDIMed/data/proc/public/unemp_pop_atappyearly.dta", keepusing(unemp_atapp pop_19_61_atapp) keep(3) nogen 
gen incidence=10^6*count/pop_age_atapp
local var unemp_atapp
reghdfe incidence c.`var' [aw=pop_19_61_atapp], abs(fipscounty) cluster(county_year)
tab covstart_year if e(sample)
qui sum `var'
gen Z`var'=(`var'-`r(mean)')/`r(sd)'
reghdfe incidence c.Z`var' [aw=pop_19_61_atapp], abs(fipscounty) cluster(county_year)

***column 4: subsample of cty X yrs we can see in ACS, LAUS measure 
merge 1:1 covstart_year fipscounty using "$SSDIMed/data/proc/public/unemp_pop_atappyearlyACS.dta"
reghdfe incidence c.`var' [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
drop Z`var'
qui sum `var' if _merge==3
gen Z`var'=(`var'-`r(mean)')/`r(sd)' if _merge==3
reghdfe incidence c.Z`var' [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
tab covstart_year _merge
tab covstart_year if e(sample)

***columns 5-8: subsample of cty X yrs we can see in ACS, ACS measure, subset ACS measures 
foreach var in unemp_nationalACS_atapp unemp_50_atapp unemp_HS_atapp{
reghdfe incidence c.`var' [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
tab covstart_year if e(sample)
qui sum `var' if _merge==3
gen Z`var'=(`var'-`r(mean)')/`r(sd)' if _merge==3
reghdfe incidence c.Z`var' [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
}
stop

***first stage for columns 5-8: outcome is unemp_atapp, otherwise keep everything the same 
reghdfe unemp_atapp c.unemp_nationalACS_atapp [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
tab covstart_year if e(sample)
reghdfe unemp_atapp c.unemp_50_atapp [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
tab covstart_year if e(sample)
reghdfe unemp_atapp c.unemp_HS_atapp [aw=pop_19_61_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
*/
stop; 

****PANEL B 
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
keep if inrange(age_year_covstart_fill, 51,52)
gcollapse (sum) count pop_age_atapp, by(covstart_month covstart_year fipscounty_firstnm county_year age_year_covstart_fill) fast
gcollapse (sum) count (mean) pop_age_atapp, by(covstart_year fipscounty_firstnm county_year age_year_covstart_fill) fast
rename fipscounty_firstnm fipscounty
merge m:1 covstart_year fipscounty using "$SSDIMed/data/proc/public/unemp_pop_atappyearly.dta", keepusing(unemp_atapp pop_19_61_atapp) keep(3) nogen 
gen incidence=10^6*count/pop_age_atapp
gen age52 = (age_year_covstart_fill == 52)
sum unemp_atapp [aw = pop_19_61_atapp]
gen UR=unemp_atapp-`r(mean)'
gen age52xUR=age52*UR
*column 3 
reghdfe incidence age52 UR age52xUR [aw=pop_age_atapp] , abs(fipscounty) cluster(county_year)
merge m:1 covstart_year fipscounty using "$SSDIMed/data/proc/public/unemp_pop_atappyearlyACS.dta", keepusing(unemp_nationalACS_atapp unemp_50_atapp unemp_HS_atapp)
*Column 4 
reghdfe incidence age52 UR age52xUR [aw=pop_age_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
***columns 5-8
foreach var in unemp_nationalACS_atapp unemp_50_atapp unemp_HS_atapp{
drop UR age52xUR 
sum `var' [aw = pop_19_61_atapp]
gen UR=`var'-`r(mean)'
gen age52xUR=age52*UR
di "Unemployment measure `var'"
reghdfe incidence age52 UR age52xUR [aw=pop_age_atapp] if _merge==3, abs(fipscounty) cluster(county_year)
}
